package com.wzdigit.wms.wms.service.basic;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.domain.basic.LocationAttr;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateUpdateListLocationAttrRequest;
import com.wzdigit.wms.basic.client.request.basic.CreateUpdateListLocationAttrRequest.CreateUpdateLocationAttrRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.LocationAttrResponse;
import com.wzdigit.wms.common.entity.StatusCode;
import com.wzdigit.wms.basic.mapper.core.basic.LocationAttrMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 库位-属性 服务类
 * @author yangdongjie
 * @LocationAttrService.java
 * @2021年1月4日上午11:16:31
 */
@Service
public class LocationAttrService {

    @Autowired
    private LocationAttrMapper locationAttrMapper;

    public <T> PagerInfo<List<LocationAttrResponse>> searchLocationAttr(
    		Integer locId, Pager<T> pager) {
    	Page<T> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
        PagerInfo<List<LocationAttrResponse>> pagerInfo = new PagerInfo<List<LocationAttrResponse>>();
        List<LocationAttrResponse> list = locationAttrMapper.selectAllByLocId(locId);
        pagerInfo.setItems(list);
        pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> createUpdateListLocationAttr(CreateUpdateListLocationAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	Date date = new Date();
    	
    	// 记录不删除的属性ID
    	List<Integer> notDelIds = new ArrayList<Integer>();
    	
    	List<CreateUpdateLocationAttrRequest> locationAttrs = dto.getLocationAttrs();
    	
    	// 去重后的大小
    	Long count = locationAttrs.stream().map(one -> one.getAttr()).distinct().count();
    	if(count.intValue() != locationAttrs.size()) {
    		Shift.fatal(StatusCode.ATTR_DATA_EXIST);
    	}
    	
    	LocationAttr locationAttr = new LocationAttr();
    	locationAttr.setLocId(dto.getLocId());
		for (CreateUpdateLocationAttrRequest one : dto.getLocationAttrs()) {
			locationAttr.setAttr(one.getAttr());
			locationAttr.setValue(one.getValue());
			locationAttr.setComments(one.getComments());
			locationAttr.setIsEnabled(one.getIsEnabled());
			if (one.getLocationAttrId() == null) {
				locationAttr.setCreateUser(loginName);
				locationAttr.setCreateTime(date);
				locationAttrMapper.insert(locationAttr);
			} else {
				locationAttr.setLocationAttrId(one.getLocationAttrId());
				locationAttr.setModifyUser(loginName);
				locationAttr.setModifyTime(date);
				locationAttrMapper.updateById(locationAttr);
			}
			notDelIds.add(locationAttr.getLocationAttrId());
		}
		
		// 删除属性
		if (notDelIds.size() > 0) {
			locationAttrMapper.deleteByLocIdAndNotId(dto.getLocId(), notDelIds);
		} else {
			locationAttrMapper.deleteByLocId(dto.getLocId());
		}
    	return new Result<>();
    }
    
    /*@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public LocationAttrResponse createLocationAttr(CreateLocationAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	LocationAttr locationAttr = locationAttrMapper.selectOneByAttrAndLocId(dto.getAttr(), dto.getLocId());
    	if(locationAttr != null) {
    		// 属性已存在
    		Shift.fatal(StatusCode.ATTR_DATA_EXIST);
    	}
    	locationAttr = new LocationAttr();
    	BeanUtil.copyNonNull(dto, locationAttr);
    	locationAttr.setCreateUser(loginName);
    	locationAttr.setCreateTime(new Date());
    	locationAttrMapper.insert(locationAttr);
    	
    	LocationAttrResponse response = new LocationAttrResponse();
    	BeanUtil.copyNonNull(locationAttr, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public LocationAttrResponse updateLocationAttr(UpdateLocationAttrRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	LocationAttr locationAttr = locationAttrMapper.selectOneByAttrAndLocId(dto.getAttr(), dto.getLocId());
    	if(locationAttr != null) {
    		if (dto.getLocationAttrId().intValue() != locationAttr.getLocationAttrId().intValue()) {
    			// 属性已存在
    			Shift.fatal(StatusCode.ATTR_DATA_EXIST);
			}
    	}
    	locationAttr = locationAttrMapper.selectByPrimaryKey(dto.getLocationAttrId());
    	BeanUtil.copy(dto, locationAttr);
    	locationAttr.setModifyUser(loginName);
    	locationAttr.setModifyTime(new Date());
    	locationAttrMapper.updateById(locationAttr);
    	
    	LocationAttrResponse response = new LocationAttrResponse();
    	BeanUtil.copy(locationAttr, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> deleteLocationAttr(Integer locationAttrId) {
    	locationAttrMapper.deleteByPrimaryKey(locationAttrId);
		return new Result<>();
    }*/
}
